from Tkinter import *
from Canvas import *
import sys
import random
from  collections import namedtuple
from matrix import *

WIDTH  = 400 # width of canvas
HEIGHT = 400 # height of canvas

HPSIZE = 1 # double of point size (must be integer)
COLOR = "#0000FF" # blue

NOPOINTS = 1000

pointList = [] # list of points (used by Canvas.delete(...))
hom_vecs = []

# neue Klasse Boundingbox um namedtuple erstellenzu koennen
Boundingbox = namedtuple('Boundingbox', 'min_x min_y min_z max_x max_y max_z')

print 
def quit(root=None):
    """ quit programm """
    if root==None:
        sys.exit(0)
    root._root().quit()
    root._root().destroy()

def draw():
    """ draw points """
    for i in range(1,NOPOINTS):
        x, y = random.randint(1,WIDTH), random.randint(1,HEIGHT)
        p = can.create_oval(x-HPSIZE, y-HPSIZE, x+HPSIZE, y+HPSIZE, fill=COLOR, outline=COLOR)
        pointList.insert(0,p)

def rotYp():
    """ rotate counterclockwise around y axis """
    global NOPOINTS
    NOPOINTS += 100
    print "In rotYp: ", NOPOINTS 
    can.delete(*pointList)
    draw()

def rotYn():
    """ rotate clockwise around y axis """
    global NOPOINTS
    NOPOINTS -= 100
    print "In rotYn: ", NOPOINTS 
    can.delete(*pointList)
    draw()

def getPointList(fil):
    """Gibt Liste von Listen zuruecke die je eine Zeile waren"""
    # split erzeugt eine liste
    return [map(float, line.split()) for line in file(raw)]

def get_bounding_box(lis):
    """Gibt eine 3D BoundingBox anhand kleinseter und groesster Koordinaten, als tuple zurueck"""
    lx = [triple[0] for triple in points]
    ly = [triple[1] for triple in points]
    lz = [triple[2] for triple in points]
    return Boundingbox(min(lx), min(ly), min(lz), max(lx), max(ly), max(lz))
    # Boundingbox ist ein collections.namedtuple

def points_to_homvec(points):
    """Macht aus Punkten homvec3 liste"""

    hom_vecs = [HomVec3(point[0], point[1], point[2], 1) for point in points]
    print hom_vecs[0:10]
    
if __name__ == "__main__":
    #check parameters
    if len(sys.argv) != 1:
       print "pointViewerTemplate.py"
       sys.exit(-1)
       
    raw = "squirrel_points.raw"
    points = getPointList(raw)
    bbox = get_bounding_box(points)
    for v in bbox:
        print v
    points_to_homvec(points)
    # create main window
    mw = Tk()

    # create and position canvas and buttons
    cFr = Frame(mw, width=WIDTH, height=HEIGHT, relief="sunken", bd=1)
    cFr.pack(side="top")
    can = Canvas(cFr, width=WIDTH, height=HEIGHT)
    can.pack()
    bFr = Frame(mw)
    bFr.pack(side="left")
    bRotYn = Button(bFr, text="<-", command=rotYn)
    bRotYn.pack(side="left")
    bRotYp = Button(bFr, text="->", command=rotYp)
    bRotYp.pack(side="left")
    eFr = Frame(mw)
    eFr.pack(side="right")
    bExit = Button(eFr, text="Quit", command=(lambda root=mw: quit(root)))
    bExit.pack()

    # draw points
    draw()

    # start
    mw.mainloop()
    
